package com.eshop.eshopmanagersystem.idal;

import com.eshop.eshopmanagersystem.model.UserInfo;
import com.eshop.eshopmanagersystem.unti.Pager;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户接口方法
 */
@Mapper
@Component
public interface IUserDAL {
    /**
     * 添加用户-由管理员添加
     *
     */
    @Insert("insert into user_tb(username,user_password,user_role,shop_name,shop_address,shop_city,shop_manager,contact)" +
            "values(#{username},#{user_password},#{user_role},#{shop_name},#{shop_address},#{shop_city},#{shop_manager},#{contact})")
    public Integer addUser(UserInfo user);

    /**
     * 后台编辑用户信息
     */
    @Update("update user_tb set user_role=#{user_role},shop_name=#{shop_name},shop_manager=#{shop_manager}," +
            "contact=#{contact},shop_address=#{shop_address},shop_city=#{shop_city} where user_id=#{user_id}")
    public Integer editUser(UserInfo user);

    /**
     * 更新用户密码
     */
    @Update("update user_tb set user_password=#{user_password} where user_id=#{user_id}")
    public Integer updateUserPass(UserInfo user);

    /**
     * 用户登录-客户端登录方法
     */
    @Select("select user_id,username,user_role from user_tb where username=#{username} and user_password=#{user_password}")
    public UserInfo validateUser(UserInfo user);

    /**
     * 后台查看用户信息分页数据
     * 参数1：startIndex
     * 参数2:pageSize
     */
    @Select("select user_id,username,user_role,shop_name,shop_manager,shop_address," +
            "shop_city,contact,create_time,last_logintime from user_tb order by user_id desc " +
            "limit #{startIndex},#{pageSize}")
    public List<UserInfo> getUsersByPager(Pager pager);

    /**
     * 后台查询用户信息分页数据总数     *
     */
    @Select("select count(*) from user_tb")
    public Integer getUsersTotalNum();

    /**
     * 带条件查询
     * 后台查看用户信息分页数据
     * 参数1：startIndex
     * 参数2:pageSize
     * 参数3：username
     */
    @Select("select user_id,username,user_role,shop_name,shop_manager,shop_address," +
            "shop_city,contact,create_time,last_logintime from user_tb where username" +
            "=#{username} order by user_id desc " +
            "limit #{startIndex},#{pageSize}")
    public List<UserInfo> getUsersByPagerAndUserName(Pager pager);

    /**
     * 带条件查询
     * 后台查询用户信息分页数据总数
     */
    @Select("select count(*) from user_tb where username=#{username}")
    public Integer getUsersTotalNumByUserName(Pager pager);

    /**
     *  用户管理
     *  批量删除用户
     *  参数：id的集合
     */
    @Delete("DELETE FROM user_tb where user_id in (${ids})")
    public Integer deleteUser(UserInfo userInfo);
}
